﻿Imports System.Reflection

''' <summary>
''' Represents a command method registered to the interpreter.
''' </summary>
''' <remarks></remarks>
Public Class RegisteredCommandMethod

    Protected Friend _ArgumentAttributes As New List(Of CommandMethodArgumentAttribute)
    ''' <summary>
    ''' Gets the array of CommandMethodArugment attributes found on this command, if any, otherwise an empty array.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ArgumentAttributes As CommandMethodArgumentAttribute()
        Get
            Return _ArgumentAttributes.ToArray
        End Get
    End Property

    Private _CommandName As String
    ''' <summary>
    ''' Gets the command name associated with the method.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property CommandName As String
        Get
            Return _CommandName
        End Get
    End Property

    ''' <summary>
    ''' Gets the help description associated with the CommandMethodArgument attribute, if any, otherwise an empty string.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Description As String
        Get
            Return _MethodAttribute.Description
        End Get
    End Property

    ''' <summary>
    ''' Gets the help formatted help text associated with the CommandMethodArgument attribute, if any, otherwise an empty string.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property FormattedHelpText As String
        Get
            Return _MethodAttribute.FormattedHelpText
        End Get
    End Property

    ''' <summary>
    ''' Gets a value determining if this command method is hidden from the help system.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property IsHidden As Boolean
        Get
            Return _MethodAttribute.IsHidden
        End Get
    End Property

    Private _MethodAttribute As CommandMethodAttribute
    ''' <summary>
    ''' Gets the CommandMethod attribute associated with this command method.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MethodAttribute As CommandMethodAttribute
        Get
            Return _MethodAttribute
        End Get
    End Property

    Private _MethodInfo As MethodInfo
    ''' <summary>
    ''' Gets the Reflection.MethodInfo which represents the delegate to invoke when processing this command method.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MethodInfo As MethodInfo
        Get
            Return _MethodInfo
        End Get
    End Property

    Private _Target As Object
    ''' <summary>
    ''' Gets the target object instance on which the delegate for this command method will be invoked.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Target As Object
        Get
            Return _Target
        End Get
    End Property

    Protected Friend Sub New(commandAttribute As CommandMethodAttribute, info As MethodInfo, target As Object, useTypeName As Boolean, argumentAttributes() As CommandMethodArgumentAttribute)
        _Target = target
        _MethodInfo = info
        _MethodAttribute = commandAttribute
        If argumentAttributes.Length > 0 Then _ArgumentAttributes.AddRange(argumentAttributes)
        Dim name As String = info.Name
        Dim typeName As String = target.GetType.Name
        If Not CommandInterpreter.CaseSensitive Then
            name = name.ToLower
            typeName = typeName.ToLower
        End If
        If useTypeName Then
            _CommandName = typeName & "." & name
        Else
            _CommandName = name
        End If
    End Sub
End Class
